package com.jianyun.jyzs.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.ProjectTrackingActivity;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmSearchBean;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.widget.LoadingDialog;
import com.jianyun.jyzs.widget.RongWebView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.jianyun.jyzs.utils.RceLog;

/**
 * 项目反馈
 * Created by yangshijun on 2018/6/4.
 */

public class ProjectTrackingFragment extends Fragment {

    @BindView(R.id.rc_web_progressbar)
    ProgressBar rcWebProgressbar;
    @BindView(R.id.rc_webview)
    public RongWebView webView;
    @BindView(R.id.feedback)
    TextView feedback;

    private LoadingDialog loadingDialog;
    private String url;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.crm_project_webview, null);
        ButterKnife.bind(this, view);
        initData();
        return view;
    }

    private void initData() {
        String userid = LoginCache.getInstance().getUserInfo().getUserId();
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        final CrmSearchBean.ListCrm poBean = (CrmSearchBean.ListCrm) getArguments().getSerializable(SysConstant.msg_key_parcelable);
        loadingDialog = LoadingDialog.create(getContext());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.setWebViewClient(new OAWebViewClient());
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDisplayZoomControls(false);
        url=erpRootUrl+"jcmh/TrackerDetailPhone.aspx"+"?employeeid="+userid+"&mainid="+poBean.getAutoid();
        RceLog.d("lml","......"+url);
        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getActivity(), ProjectTrackingActivity.class);
                intent.putExtra(SysConstant.msg_key_parcelable,poBean);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        loadUrl(url);
    }

    public void loadUrl(String url){
        if (webView!=null){
            webView.loadUrl(url);
        }
    }

    class OAWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            loadingDialog.show();
            //view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            loadingDialog.dismiss();
        }
    }

}
